package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.*;
import com.woniuxy.dudugua.mapper.ProductScanMapper;
import com.woniuxy.dudugua.repo.ProductESRepo;
import com.woniuxy.dudugua.service.ProductService;
import com.woniuxy.dudugua.service.impl.*;
import com.woniuxy.dudugua.util.Page;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.view.ProductInfoView;
import com.woniuxy.dudugua.util.view.ProductView;
import com.woniuxy.dudugua.util.view.SingleProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("product")
public class ProductController {
    @Resource
    private ProductServiceImpl productServiceImpl;
    @Resource
    private ProductImagesServiceImpl productImagesServiceImpl;
    @Resource
    private ProductInfoImagesServiceImpl productInfoImagesServiceImpl;
    @Resource
    private ProductSkuServiceImpl productSkuServiceImpl;
    @Resource
    private CommentServiceImpl commentServiceImpl;
    @Resource
    private ProductInfoServiceImpl productInfoServiceImpl;
    @Resource
    private ProductScanServiceImpl productScanServiceImpl;
    /*@Resource
    private ProductESRepo productESRepo;*/

    @RequestMapping("getAll")
    public Result getAllProducts(Integer pageIndex, Integer pageNumber){
//        System.out.println(pageNo);
//        System.out.println(pageSize);
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("pageIndex",pageIndex);
        params.put("pageNumber",pageNumber);
        long dataCount = productServiceImpl.countByExample(null);
        long pageCount = dataCount%pageNumber==0?dataCount/pageNumber:dataCount/pageNumber+1;
        Page page = new Page(pageIndex,dataCount,pageCount,pageNumber);
        return new Result("200","success",page,productServiceImpl.selectByExample(params));
    }

    @RequestMapping("search")
    public Result searchProducts(String kw){
        System.out.println(kw);
//        return new Result("200","success",null,productESRepo.search(kw));
        return null;
    }

    @RequestMapping("getProduct")
    public Result getProductById(Integer id){
        ProductInfoView productInfo = productServiceImpl.getProductById(id);
        List<ProductImages> images = productImagesServiceImpl.selectById(id);
        List<ProductInfoImages> infoImages = productInfoImagesServiceImpl.selectById(id);
        List<ProductSku> productSkus = productSkuServiceImpl.selectByProductId(id);
        List<Comment> comments = commentServiceImpl.selectByProductId(id);
        List<ProductScan> productScans = productScanServiceImpl.selectByProductId(id);
        String introduce = productInfoServiceImpl.selectById(id).getIntroduce();
        int number = 0;
        for (ProductSku ps: productSkus) {
            number = number + ps.getNumber();
        }
        int times = 0;
        for (ProductScan p: productScans) {
            times = times+p.getTimes();
        }
        SingleProduct singleProduct = new SingleProduct();
        singleProduct.setProductInfo(productInfo);
        singleProduct.setImages(images);
        singleProduct.setInfoImages(infoImages);
        singleProduct.setIntroduce(introduce);
        singleProduct.setComments(comments);
        singleProduct.setProductSkus(productSkus);
        singleProduct.setNumber(number);
        singleProduct.setTimes(times);
        return new Result("200","success",singleProduct,null);
    }

    @RequestMapping("getPro")
    public Result getPro(){
        return new Result("200","success",null,productServiceImpl.getPro());
    }

    @RequestMapping("add")
    public Result add(){
        int id = 1;
        String productName = "眼膜贴淡化细纹补水紧致去黑眼圈眼袋眼贴60片眼贴膜";
        String storeName = "DUDU自营";
        int storeId = 2;
        double price = 39.00;
        String  url = "https://a.vpimg3.com/upload/merchandise/pdcvis/110218/2020/0224/66/1d11650d-73e1-4fec-b2ea-9c7e08fa1dfe.jpg";
        int sales = 100;
        ProductView productView = new ProductView(id, productName, storeName, storeId, price, url, sales);
//        return new Result("200","success",productESRepo.save(productView),null);
        return null;
    }
}
